﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WeatherComm
{
    public partial class WeatherData
    {
        //public string City { get; set; }
        //public float High { get; set; }
        //public float Low { get; set; }
        public static List<CityInfo2> GetFromDb()
        {
            try
            {
                var connectString = ConfigurationManager.AppSettings["Connection"];
                var sql = $@"SELECT  * FROM [dbo].[WeatherData]";

                using (var conn = new SqlConnection(connectString))
                {
                    conn.Open();
                    var adapter = new SqlDataAdapter(sql, conn);
                    var db = new DataTable();
                    adapter.Fill(db);

                    var ls = new List<CityInfo2>();

                    foreach (DataRow r in db.Rows)
                    {
                        var a = new CityInfo2
                        {
                            city_code = r["CityKey"].ToString(),
                            city_name = r["City"].ToString(),
                            Enable = r["Enable"].ToString() == "True"
                        };
                        if (a.Enable)
                        {
                            ls.Add(a);
                        }

                    }

                    db.Dispose();
                    conn.Close();
                    adapter.Dispose();

                    return ls;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
}